<?php
/**
 * 本地招商项目
 */
namespace app\model;

use think\Model;
use app\model\Chain;

class LocalprojectsModel extends Model
{
	protected $name = 'wqq_game_localprojects';
	/**
	 * 本地招商项目列表
	 *
	 */
	public function GetList($where){
		$LocalprojectsModel=new LocalprojectsModel;
		$result=$LocalprojectsModel->field('id,name,desc,city_name,address,update_time,status,code_url,imgs')->where($where)->order('id desc')->paginate()->toArray();
		return $result;
	}
	/**
	 * 本地招商项目详情
	 *
	 */
	public function detail($id){
		$LocalprojectsModel = LocalprojectsModel::find($id);
		return $LocalprojectsModel;
	}
	/**
	 * 添加
	 *
	 */
	public function AddData($param){
		$LocalprojectsModel = new LocalprojectsModel;
		$param['create_time']=time();
		$param['update_time']=time();
	    $param['uniacid']=config('app.wxapp_uniacid');
		/* if(isset($param['chain_id'])&&!empty($param['chain_id'])){
			$ChainModel=new Chain();
			$ids=explode(',', $param['chain_id']);
			$chain_id=[];
			foreach ($ids as $val){
				$chain_id[]=$ChainModel->GetChainIdById($val);
			}		
			$param['chain_id']=implode(',', $chain_id);
		} */
		$field=['uniacid','name','desc','address','video','imgs','content','create_time','update_time','role_id','chain_id'];
		$LocalprojectsModel->allowField($field)->save($param);
		$id=$LocalprojectsModel->id;
		
		//生成小程序二维码
		
		$data = array(
				'scene' => $id,
				'page' => 'pages/localproject/detail',
				'width' => 280,
		);	
		$url= construct_XCX_Code($data,config('app.wxapp_uniacid'));
		$LocalprojectsModel->update(['code_url'=>$url,'id'=>$id]);
		return $id;
	}
	/**
	 * 编辑
	 *
	 */
	public function EditData($param,$id){
		$LocalprojectsModel = LocalprojectsModel::find($id);
		$param['update_time']=time();
		/* if(isset($param['chain_id'])&&!empty($param['chain_id'])){
			$ChainModel=new Chain();
			$ids=explode(',', $param['chain_id']);
			$chain_id=[];
			foreach ($ids as $val){
				$chain_id[]=$ChainModel->GetChainIdById($val);
			}
			$param['chain_id']=implode(',', $chain_id);
		} */
		
		$field=['name','desc','address','video','imgs','content','create_time','update_time','role_id','chain_id'];
		
		$LocalprojectsModel->allowField($field)->save($param);
		
		
		//生成小程序二维码
		$data = array(
				'scene' => $id,
				'page' => 'pages/localproject/detail',
				'width' => 280,
		);
		$url= construct_XCX_Code($data,config('app.wxapp_uniacid'));
		$LocalprojectsModel->update(['code_url'=>$url,'id'=>$id]);
		return $id;
	}
	/**
	 * 修改禁用状态
	 *
	 */
	public function UpdateStatus($id){
		$LocalprojectsModel = LocalprojectsModel::find($id);
		$LocalprojectsModel->status=$LocalprojectsModel->status==1?0:1;
		$LocalprojectsModel->save();
		return true;
	}
	/**
	 * 删除
	 *
	 */
	public function Del($param){
		$update=[];
		foreach($param as $val){
			$update[]=['id'=>$val,'status'=>-1,'deletetime'=>time()];
		}
		if(!empty($update)){
			$LocalprojectsModel = new LocalprojectsModel;
			$LocalprojectsModel->saveAll($update);
		}
		return true;
	}
	/**
	 * 本地招商项目统计
	 *
	 */
	public function GetTotal($where){
		$LocalprojectsModel=new LocalprojectsModel;
		$result=$LocalprojectsModel->where($where)->count();
		return $result;
	}
}